package algorithm.tr;// Java implementation to determine the
// Minimum numbers of steps to make
// median of an array equal X

import java.util.*;
import java.util.stream.Collectors;

public class Gfg {

    // Function to count minimum
// required operations to
// make median X
    public static int count(int[] a, int X) {

        // Sorting the array a[]
        Arrays.sort(a);
        int ans = 0;

        // Calculate the size of array
        int n = a.length;

        // Iterate over the array
        for (int i = 0; i < n; i++) {

            // For all elements
            // less than median
            if (i < n / 2) {
                ans += Math.max(0, a[i] - X);
            }

                // For element equal
                // to median
            else if (i == n / 2) {
                ans += Math.abs(X - a[i]);
            }

                // For all elements
                // greater than median
            else {
                ans += Math.max(0, X - a[i]);
            }
        }

        // Return the answer
        return ans;
    }

    // Driver code
    public static void main(String[] args) {
        int[] a = {6, 5, 8};
        int X = 8;
        new ArrayList<>().stream().sorted().collect(Collectors.toList());
        System.out.print(count(a, X) + "\n");
    }
}
